package comment

import (
	"github.com/gin-gonic/gin"
	. "myplatform/handler"
	"myplatform/libs/errcode"
	"myplatform/libs/valiutils"
	"myplatform/model"
	"strconv"
)

// 根据评论找user
func GetUserBycomment(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		//自定义错误码
		SendRsp(c, errcode.ParamTypeError, nil)
		return
	}
	var m model.Comment
	m.Cid = uint64(id)
	if err := m.GetUserByComment(); err != nil {
		SendRsp(c, errcode.SearchError, nil)
		return
	}
	SendRsp(c, errcode.OK, m.User)
}

// 添加评论
func Addcomment(c *gin.Context) {
	var s model.Comment
	//绑定传来的json，相当于进行s的赋值
	if err := c.ShouldBindJSON(&s); err != nil {
		SendRsp(c, errcode.ParamBindError, s)
		return
	}
	//验证器
	//validate:=validator.New()
	if err := valiutils.ValidateObj.Struct(&s); err != nil {
		SendRsp(c, errcode.DataValidatorError, nil)
		return
	}

	if err := s.Create(); err != nil {
		SendRsp(c, errcode.WriteError, nil)
		return
	}
	SendRsp(c, errcode.OK, s)
}
